package com.hxd.mall.utils.open

import android.app.Activity
import android.view.View
import androidx.core.content.ContextCompat
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.listener.OnOptionsSelectListener
import com.bigkoo.pickerview.view.OptionsPickerView
import com.hxd.mall.R
import com.hxd.mall.network.base.BaseBean
import com.hxd.mall.utils.local.BuriedPointManageUtil.tagIsEmpty
import java.io.IOException
import java.io.InputStream
import java.util.*

/**
 * OptionsPickerView扩展
 * 下拉框
 */
object DropdownSelectionUtil {

    fun dropdownSelectionUtils(view: View, activity: Activity, arrayList: MutableList<String>, defaultIndex: Int?, callbacks: (String, Int) -> Unit) {
        dropdownSelectionUtils(view, activity, arrayList, defaultIndex, callbacks, null)
    }

    fun dropdownSelectionUtils(view: View, activity: Activity, arrayList: MutableList<String>, defaultIndex: Int?, callbacks: (String, Int) -> Unit, callbackDismiss: (() -> Unit)?) {
        KeyboardUtil.hideKeyBoardActivity(activity)
        val optionsPickerView: OptionsPickerView<String> = OptionsPickerBuilder(activity,
                OnOptionsSelectListener { options1, _, _, _ ->
                    callbacks(arrayList[options1], options1)
                    tagIsEmpty(view) {
                        //LoganUtil.clickEndTime(it)
                    }
                })

                .setCancelColor(ContextCompat.getColor(activity, R.color.color_666666))
                .setSubmitColor(ContextCompat.getColor(activity, R.color.color_1153FC))
                .setDividerColor(ContextCompat.getColor(activity, R.color.transparent))
                .setTextColorCenter(ContextCompat.getColor(activity, R.color.color_1153FC))
                .setTextColorOut(ContextCompat.getColor(activity, R.color.color_999999))
                .setContentTextSize(18)
                .setSelectOptions(if (defaultIndex == null || defaultIndex < 0) 0 else defaultIndex)
                .setDecorView(activity.window.decorView.findViewById(android.R.id.content))
                .build()
        if (callbackDismiss != null) {
            optionsPickerView.setOnDismissListener {
                callbackDismiss()
            }
        }
        optionsPickerView.setNPicker(arrayList, null, null)
        optionsPickerView.show()
    }

    fun dropdownSelectionUtils(activity: Activity, startYear: Int, endYear: Int, firstIndex: Int?, secondIndex: Int?, callbacks: (Int, Int) -> Unit) {
        KeyboardUtil.hideKeyBoardActivity(activity)
        val year = mutableListOf<String>()
        for (i in startYear..endYear) {
            year.add(i.toString())
        }
        val month = arrayListOf("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11")
        val optionsPickerView: OptionsPickerView<String> = OptionsPickerBuilder(activity,
                OnOptionsSelectListener { options1, options2, _, _ ->
                    callbacks(options1, options2)
//                    tagIsEmpty(view) {
                    //LoganUtil.clickEndTime(it)
//                    }
                })
                .setCancelColor(ContextCompat.getColor(activity, R.color.color_666666))
                .setSubmitColor(ContextCompat.getColor(activity, R.color.color_1153FC))
                .setDividerColor(ContextCompat.getColor(activity, R.color.transparent))
                .setTextColorCenter(ContextCompat.getColor(activity, R.color.color_1153FC))
                .setTextColorOut(ContextCompat.getColor(activity, R.color.color_999999))
                .setContentTextSize(16)
                .setSelectOptions(firstIndex ?: 0, secondIndex ?: 0)
                .setLabels("年", "个月", "")
                .setDecorView(activity.window.decorView.findViewById(android.R.id.content))
                .build()
        optionsPickerView.setNPicker(year.toMutableList(), month, null)
        optionsPickerView.show()
    }

    fun dropdownSelectionUtils(activity: Activity, callbacks: (CheckItem) -> Unit) {
        KeyboardUtil.hideKeyBoardActivity(activity)
        var inputStream: InputStream? = null
        try {
//            inputStream = activity.resources.assets.open("list_self.json")
//            val buffer = ByteArray(inputStream!!.available())
//            inputStream.read(buffer)
//            val cityStr = String(buffer, charset("utf-8"))
//            val cityBean = GsonUtil.json2bean(cityStr, NewBean::class.java)
            inputStream = activity.resources.assets.open("lists.json")
            val buffer = ByteArray(inputStream!!.available())
            inputStream.read(buffer)
            val cityStr = String(buffer, charset("utf-8"))
            val cityBean = GsonUtil.json2bean(cityStr, ProvinceItem::class.java)

            val provinceList = mutableListOf<ProvinceItem.CityItem>()

            //将解析的集合分解
            val province = mutableListOf<String>()
            val city = mutableListOf<MutableList<String>>()
            val subcity = mutableListOf<MutableList<MutableList<String>>>()
            cityBean.provinceInfo.forEach { cityItem ->
                provinceList.add(cityItem)
                province.add(cityItem.name)

                val citys = mutableListOf<MutableList<String>>()
                val mutableListOf = mutableListOf<String>()
                cityItem.subLists.forEach { countyItem ->
                    mutableListOf.add(countyItem.name)

                    val airs = mutableListOf<String>()
                    countyItem.countys.forEach { airItem ->
                        airs.add(airItem.name)
                    }
                    citys.add(airs)
                }
                city.add(mutableListOf)

                subcity.add(citys)
            }

            val optionsPickerView: OptionsPickerView<String> =
                    OptionsPickerBuilder(activity,
                            OnOptionsSelectListener { options1, options2, options3, v ->

                                val provinceItem = provinceList[options1]
                                val countyItem = provinceItem.subLists[options2]

                                //有些市有可能没有录入区/县，例如河北/定州，传默认值
                                var airName = ""
                                var airId = "0"
                                if (countyItem.countys.isNotEmpty()) {
                                    countyItem.countys[options3].run {
                                        airName = name
                                        airId = id.toString()
                                    }
                                }
                                callbacks(CheckItem(provinceItem.name, provinceItem.id.toString(), countyItem.name, countyItem.id.toString(), airName, airId))
//                                tagIsEmpty(view) {
//                                    LoganUtil.clickEndTime(it)
//                                }
                            })
                            .setCancelColor(ContextCompat.getColor(activity, R.color.color_666666))
                            .setSubmitColor(ContextCompat.getColor(activity, R.color.color_1153FC))
                            .setDividerColor(ContextCompat.getColor(activity, R.color.transparent))
                            .setTextColorCenter(ContextCompat.getColor(activity, R.color.color_1153FC))
                            .setTextColorOut(ContextCompat.getColor(activity, R.color.color_999999))
                            .setContentTextSize(16)
                            .setDecorView(activity.window.decorView.findViewById(android.R.id.content))
                            .build()
//            optionsPickerView.setPicker(
//                    cityBean.province,
//                    cityBean.city as List<MutableList<String>>?,
//                    cityBean.subcity as List<MutableList<MutableList<String>>>?
//            )
            optionsPickerView.setPicker(
                    province,
                    city,
                    subcity
            )
            optionsPickerView.show()
        } catch (e: IOException) {
            e.printStackTrace()
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        }
    }

    data class NewBean(
        var province: ArrayList<String>,
        var city: ArrayList<ArrayList<String>>,
        var subcity: ArrayList<ArrayList<ArrayList<String>>>
    ) : BaseBean()

    data class ProvinceItem(var provinceInfo: ArrayList<CityItem>) : BaseBean() {
        data class CityItem(
            var id: Int,
            var level: Int,
            var name: String,
            var parent: Int,
            var subLists: ArrayList<CountyItem>
        ) : BaseBean() {
            data class CountyItem(
                var countys: ArrayList<AirItem>,
                var id: Int,
                var level: Int,
                var name: String,
                var parent: Int
            ) : BaseBean() {
                data class AirItem(var id: Int, var level: Int, var name: String, var parent: Int) :
                    BaseBean()
            }
        }
    }

    //选中的item
    data class CheckItem(
        var province: String,
        var provinceId: String,
        var city: String,
        var cityId: String,
        var county: String,
        var countyId: String
    ) : BaseBean()
}
